package com.ubst.smartad.impl;

import java.sql.*;
import java.util.*;
import java.io.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSASysGroupPolicyOPTDO;
import com.ubst.smartad.helper.CryptHelper;
import com.ubst.smartad.common.TimeUtil;
import com.ubst.smartad.logger.Debug;

public class TSASysGroupPolicyOPTMngImpl {
	
	Connection con = null;
	private CryptHelper chelper;
	private String serverName;
	private Debug debug;

	/** 
     * 생성자
     *   
     *   
    */ 
	public TSASysGroupPolicyOPTMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.chelper = new CryptHelper();
		this.serverName = serverName;
		this.debug = new Debug("TSASysGroupPolicyOPTMngImpl", log_level);

	}
	
	/** 
	 * @author KJH
     * 계정관리 그룹 기본정책 등록
     *   
     */  
    public void insertGroupPolicyOPT(TSASysGroupPolicyOPTDO policyOPTDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_SYSGROUP_POLICYOPT " )
				.append(" ( ")
				.append("  GROUPID ,PASSMAXDAY ,PASSMINDAY ,PASSWARN ,INACTIVEDAY ")
				.append(" ,LOGINRETRY ,RETRYRESET ,SUSPENDTIME ,UNUSEDTIME ,SESSIONCNT ")
				.append(" ,SESSIONTIMEOUT ,PASSMINLEN ,PASSMAXLEN ,PASSHISTORY ,PASSMINALPHA ")
				.append(" ,PASSMINDIGIT ,PASSMINUALPHA ,PASSMINLALPHA ,PASSMINSPECIAL ,PASSMINDIFF ")
				.append(" ,PASSMAXSEQ ,PASSMAXREPEAT ,DEFAULTHOME ,DEFAULTSHELL ,CRESULT ")
				.append(" ,NSTATUS ,LASTSYNCDATE ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ")
				.append(" ) ");

			debug.debug("insertGroupPolicyOPT ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, policyOPTDO.groupid);
			pstmt.setInt(idx++, policyOPTDO.passmaxday);
			pstmt.setInt(idx++, policyOPTDO.passminday);
			pstmt.setInt(idx++, policyOPTDO.passwarn);
			pstmt.setInt(idx++, policyOPTDO.inactiveday);
			pstmt.setInt(idx++, policyOPTDO.loginretry);
			pstmt.setInt(idx++, policyOPTDO.retryreset);
			pstmt.setInt(idx++, policyOPTDO.suspendtime);
			pstmt.setInt(idx++, policyOPTDO.unusedtime);
			pstmt.setInt(idx++, policyOPTDO.sessioncnt);
			pstmt.setInt(idx++, policyOPTDO.sessiontimeout);
			pstmt.setInt(idx++, policyOPTDO.passminlen);
			pstmt.setInt(idx++, policyOPTDO.passmaxlen);
			pstmt.setInt(idx++, policyOPTDO.passhistory);
			pstmt.setInt(idx++, policyOPTDO.passminalpha);
			pstmt.setInt(idx++, policyOPTDO.passmindigit);
			pstmt.setInt(idx++, policyOPTDO.passminualpha);
			pstmt.setInt(idx++, policyOPTDO.passminlalpha);
			pstmt.setInt(idx++, policyOPTDO.passminspecial);
			pstmt.setInt(idx++, policyOPTDO.passmindiff);
			pstmt.setInt(idx++, policyOPTDO.passmaxseq);
			pstmt.setInt(idx++, policyOPTDO.passmaxrepeat);
			pstmt.setString(idx++, policyOPTDO.defaulthome);
			pstmt.setString(idx++, policyOPTDO.defaultshell);
			pstmt.setString(idx++, policyOPTDO.cresult);
			pstmt.setInt(idx++, policyOPTDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

    /** 
	 * @author KJH
     * 계정관리 그룹 기본정책 정보 변경
     *   
     */  
    public void updateGroupPolicyOPT(TSASysGroupPolicyOPTDO policyOPTDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_SYSGROUP_POLICYOPT " )
				.append(" SET ")
				.append("  PASSMAXDAY=? ,PASSMINDAY=? ,PASSWARN=? ,INACTIVEDAY=? ,LOGINRETRY=? ")
				.append(" ,RETRYRESET=? ,SUSPENDTIME=? ,UNUSEDTIME=? ,SESSIONCNT=? ,SESSIONTIMEOUT=? ")
				.append(" ,PASSMINLEN=? ,PASSMAXLEN=? ,PASSHISTORY=? ,PASSMINALPHA=? ,PASSMINDIGIT=? ")
				.append(" ,PASSMINUALPHA=? ,PASSMINLALPHA=? ,PASSMINSPECIAL=? ,PASSMINDIFF=? ,PASSMAXSEQ=? ")
				.append(" ,PASSMAXREPEAT=? ,DEFAULTHOME=? ,DEFAULTSHELL=? ,CRESULT=? ,NSTATUS=? ")
				.append(" ,LASTSYNCDATE=? ")
				.append(" WHERE GROUPID=? ");

			debug.debug("updateGroupPolicyOPT ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, policyOPTDO.passmaxday);
			pstmt.setInt(idx++, policyOPTDO.passminday);
			pstmt.setInt(idx++, policyOPTDO.passwarn);
			pstmt.setInt(idx++, policyOPTDO.inactiveday);
			pstmt.setInt(idx++, policyOPTDO.loginretry);
			pstmt.setInt(idx++, policyOPTDO.retryreset);
			pstmt.setInt(idx++, policyOPTDO.suspendtime);
			pstmt.setInt(idx++, policyOPTDO.unusedtime);
			pstmt.setInt(idx++, policyOPTDO.sessioncnt);
			pstmt.setInt(idx++, policyOPTDO.sessiontimeout);
			pstmt.setInt(idx++, policyOPTDO.passminlen);
			pstmt.setInt(idx++, policyOPTDO.passmaxlen);
			pstmt.setInt(idx++, policyOPTDO.passhistory);
			pstmt.setInt(idx++, policyOPTDO.passminalpha);
			pstmt.setInt(idx++, policyOPTDO.passmindigit);
			pstmt.setInt(idx++, policyOPTDO.passminualpha);
			pstmt.setInt(idx++, policyOPTDO.passminlalpha);
			pstmt.setInt(idx++, policyOPTDO.passminspecial);
			pstmt.setInt(idx++, policyOPTDO.passmindiff);
			pstmt.setInt(idx++, policyOPTDO.passmaxseq);
			pstmt.setInt(idx++, policyOPTDO.passmaxrepeat);
			pstmt.setString(idx++, policyOPTDO.defaulthome);
			pstmt.setString(idx++, policyOPTDO.defaultshell);
			pstmt.setString(idx++, policyOPTDO.cresult);
			pstmt.setInt(idx++, policyOPTDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));
			pstmt.setString(idx++, policyOPTDO.groupid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

	/**
	 * @author : KJH 
	 * 계정관리 그룹 기본정책 정보 출력
	 *
	 */
	public TSASysGroupPolicyOPTDO selectGroupPolicyOPT(String groupid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSASysGroupPolicyOPTDO policyOPTDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  PASSMAXDAY ,PASSMINDAY ,PASSWARN ,INACTIVEDAY ,LOGINRETRY ")
				.append(" ,RETRYRESET ,SUSPENDTIME ,UNUSEDTIME ,SESSIONCNT ,SESSIONTIMEOUT ")
				.append(" ,PASSMINLEN ,PASSMAXLEN ,PASSHISTORY ,PASSMINALPHA ,PASSMINDIGIT ")
				.append(" ,PASSMINUALPHA ,PASSMINLALPHA ,PASSMINSPECIAL ,PASSMINDIFF ,PASSMAXSEQ ")
				.append(" ,NSTATUS ,PASSMAXREPEAT ,DEFAULTHOME ,DEFAULTSHELL ,CRESULT ")
				.append(" ,LASTSYNCDATE ")
				.append(" FROM TSA_SYSGROUP_POLICYOPT ")
				.append(" WHERE GROUPID=? ");

			debug.debug("selectGroupPolicyOPT ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, groupid);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				policyOPTDO = new TSASysGroupPolicyOPTDO();
				policyOPTDO.groupid = groupid;
				policyOPTDO.passmaxday = rs.getInt("PASSMAXDAY");
				policyOPTDO.passminday = rs.getInt("PASSMINDAY");
				policyOPTDO.passwarn = rs.getInt("PASSWARN");
				policyOPTDO.inactiveday = rs.getInt("INACTIVEDAY");
				policyOPTDO.loginretry = rs.getInt("LOGINRETRY");
				policyOPTDO.retryreset = rs.getInt("RETRYRESET");
				policyOPTDO.suspendtime = rs.getInt("SUSPENDTIME");
				policyOPTDO.unusedtime = rs.getInt("UNUSEDTIME");
				policyOPTDO.sessioncnt = rs.getInt("SESSIONCNT");
				policyOPTDO.sessiontimeout = rs.getInt("SESSIONTIMEOUT");
				policyOPTDO.passminlen = rs.getInt("PASSMINLEN");
				policyOPTDO.passmaxlen = rs.getInt("PASSMAXLEN");
				policyOPTDO.passhistory = rs.getInt("PASSHISTORY");
				policyOPTDO.passminalpha = rs.getInt("PASSMINALPHA");
				policyOPTDO.passmindigit = rs.getInt("PASSMINDIGIT");
				policyOPTDO.passminualpha = rs.getInt("PASSMINUALPHA");
				policyOPTDO.passminlalpha = rs.getInt("PASSMINLALPHA");
				policyOPTDO.passminspecial = rs.getInt("PASSMINSPECIAL");
				policyOPTDO.passmindiff = rs.getInt("PASSMINDIFF");
				policyOPTDO.passmaxseq = rs.getInt("PASSMAXSEQ");
				policyOPTDO.nstatus = rs.getInt("NSTATUS");
				policyOPTDO.passmaxrepeat = rs.getInt("PASSMAXREPEAT");
				policyOPTDO.defaulthome = rs.getString("DEFAULTHOME");
				policyOPTDO.defaultshell = rs.getString("DEFAULTSHELL");
				policyOPTDO.cresult = rs.getString("CRESULT");
				policyOPTDO.lastsyncdate = rs.getString("LASTSYNCDATE");

			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return policyOPTDO;
	}

    /** 
	 * @author KJH
     * 계정관리 그룹 기본정책 삭제
     *   
     */  
    public void deleteGroupPolicyOPT(String groupid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_SYSGROUP_POLICYOPT " )
				.append(" WHERE GROUPID=? ");

			debug.debug("deleteGroupPolicyOPT ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, groupid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

}
